package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.TbDevice;
import com.ruoyi.system.domain.TbRoom;
import com.ruoyi.system.domain.TbSt;
import com.ruoyi.system.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TbRoomDevice;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 房间设备学生成绩记录Controller
 * 
 * @author ruoyi
 * @date 2025-10-25
 */
@RestController
@RequestMapping("/roomdevice/device")
public class TbRoomDeviceController extends BaseController
{
    @Autowired
    private ITbRoomDeviceService tbRoomDeviceService;

    @Autowired
    ITbRoomService tbRoomService;

    @Autowired
    ITbStService tbStService;

    @Autowired
    ITbDeviceService tbDeviceService;

    @Autowired
    ISysUserService sysUserService;

    /**
     * 查询房间设备学生成绩记录列表
     */
    @PreAuthorize("@ss.hasPermi('roomdevice:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbRoomDevice tbRoomDevice)
    {
        startPage();
        List<TbRoomDevice> list = tbRoomDeviceService.selectTbRoomDeviceList(tbRoomDevice);
        for (TbRoomDevice roomDevice : list) {
            if (roomDevice.getDeviceId() != null) {
                TbDevice tbDevice = tbDeviceService.selectTbDeviceById(Integer.valueOf(roomDevice.getDeviceId().toString()));
                if (tbDevice != null) {
                    roomDevice.setTbDevice(tbDevice);
                    roomDevice.setDeviceName(tbDevice.getName());
                }
            }
            if (roomDevice.getRoomId() != null) {
                TbRoom tbRoom = tbRoomService.selectTbRoomById(roomDevice.getRoomId());
                if (tbRoom != null) {
                    roomDevice.setTbRoom(tbRoom);
                    roomDevice.setRoomName(tbRoom.getName());
                }
            }
            if (roomDevice.getStId() != null) {
                TbSt tbSt = tbStService.selectTbStById(roomDevice.getStId());
                if (tbSt != null) {
                    roomDevice.setTbSt(tbSt);
                    roomDevice.setStName(tbSt.getCode());
                }
            }
            if (roomDevice.getUserId() != null) {
                SysUser sysUser = sysUserService.selectUserById(roomDevice.getUserId());
                if (sysUser != null) {
                    roomDevice.setSysUser(sysUser);
                    roomDevice.setUserName(sysUser.getUserName());
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出房间设备学生成绩记录列表
     */
    @PreAuthorize("@ss.hasPermi('roomdevice:device:export')")
    @Log(title = "房间设备学生成绩记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbRoomDevice tbRoomDevice)
    {
        List<TbRoomDevice> list = tbRoomDeviceService.selectTbRoomDeviceList(tbRoomDevice);
        ExcelUtil<TbRoomDevice> util = new ExcelUtil<TbRoomDevice>(TbRoomDevice.class);
        util.exportExcel(response, list, "房间设备学生成绩记录数据");
    }

    /**
     * 获取房间设备学生成绩记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('roomdevice:device:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        TbRoomDevice tbRoomDevice = tbRoomDeviceService.selectTbRoomDeviceById(id);
        if (tbRoomDevice.getDeviceId() != null) {
            tbRoomDevice.setTbDevice(tbDeviceService.selectTbDeviceById(Integer.valueOf(tbRoomDevice.getDeviceId().toString())));
        }
        if (tbRoomDevice.getRoomId() != null) {
            tbRoomDevice.setTbRoom(tbRoomService.selectTbRoomById(tbRoomDevice.getRoomId()));
        }
        if (tbRoomDevice.getStId() != null) {
            tbRoomDevice.setTbSt(tbStService.selectTbStById(tbRoomDevice.getStId()));
        }
        if (tbRoomDevice.getUserId() != null) {
            tbRoomDevice.setSysUser(sysUserService.selectUserById(tbRoomDevice.getUserId()));
        }
        return success(tbRoomDevice);
    }

    /**
     * 新增房间设备学生成绩记录
     */
    @PreAuthorize("@ss.hasPermi('roomdevice:device:add')")
    @Log(title = "房间设备学生成绩记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbRoomDevice tbRoomDevice)
    {
        return toAjax(tbRoomDeviceService.insertTbRoomDevice(tbRoomDevice));
    }

    /**
     * 修改房间设备学生成绩记录
     */
    @PreAuthorize("@ss.hasPermi('roomdevice:device:edit')")
    @Log(title = "房间设备学生成绩记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbRoomDevice tbRoomDevice)
    {
        return toAjax(tbRoomDeviceService.updateTbRoomDevice(tbRoomDevice));
    }

    /**
     * 删除房间设备学生成绩记录
     */
    @PreAuthorize("@ss.hasPermi('roomdevice:device:remove')")
    @Log(title = "房间设备学生成绩记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbRoomDeviceService.deleteTbRoomDeviceByIds(ids));
    }
}
